package com.liliudong.keelflower.upms.admin.handler;

import com.liliudong.keelflower.common.core.constants.HttpStatus;
import com.liliudong.keelflower.common.core.exception.BusinessException;
import com.liliudong.keelflower.common.core.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.Set;

/**
 * @author Liudong.li
 */
@Slf4j
@RestControllerAdvice
public class BusinessExceptionHandler {
    /**
     * BusinessException 拦截
     *
     * @param e
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    public R<?> businessException(BusinessException e) {
        HttpStatus httpStatus = e.getHttpStatus();
        return R.fail(httpStatus);
    }

    /**
     * 权限不足
     *
     * @param e
     * @return
     */
    @ExceptionHandler(UnauthorizedException.class)
    public R<?> authenticationException(UnauthorizedException e) {
        log.error(e.getMessage(), e);
        return R.fail(HttpStatus.UNKNOWN_ACCOUNT);
    }

    /**
     * 参数校验异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public R<?> methodArgumentNotValidException(MethodArgumentNotValidException e) {
        StringBuilder errorInfo = new StringBuilder();
        BindingResult bindingResult = e.getBindingResult();
        for (int i = 0; i < bindingResult.getFieldErrors().size(); i++) {
            if (i > 0) {
                errorInfo.append(",");
            }
            FieldError fieldError = bindingResult.getFieldErrors().get(i);
            errorInfo.append(fieldError.getDefaultMessage());
        }
        String errorMessage = errorInfo.toString();
        log.error(errorMessage, e);
        return R.fail(errorMessage);
    }

    /**
     * 参数校验异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(ConstraintViolationException.class)
    @ResponseStatus(org.springframework.http.HttpStatus.BAD_REQUEST)
    public R<?> constraintViolationException(ConstraintViolationException e) {
        StringBuilder errorInfo = new StringBuilder();
        String errorMessage;
        Set<ConstraintViolation<?>> violations = e.getConstraintViolations();
        for (ConstraintViolation<?> item : violations) {
            errorInfo.append(item.getMessage()).append(",");
        }
        errorMessage = errorInfo.substring(0, errorInfo.toString().length() - 1);
        log.error(errorMessage, e);
        return R.fail(errorMessage);
    }

    /**
     * 全局异常捕获
     *
     * @return
     */
    @ExceptionHandler(Exception.class)
    public R<?> exception(Exception e) {
        log.error(e.getMessage(), e);
        return R.fail(HttpStatus.SYSTEM_BUSY);
    }
}
